<template>
  <!--登录页面整体的盒子 -->
  <div class="login-container">
    <!-- 登录盒子 -->
    <div class="login-box">
      <!-- 标题盒子 -->
      <div class="title-box">
      </div>
      <!-- 表单盒子 -->
      <el-form ref="form" :model="form" :rules="rules">
        <!-- 注册名称 -->
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="请输入注册的名称"></el-input>
        </el-form-item>
        <!-- 注册密码 -->
        <el-form-item prop='password'>
          <el-input v-model="form.password" type="password" placeholder="请输入注册的密码"></el-input>
        </el-form-item>
        <!-- 重复密码 -->
        <el-form-item prop="repassword">
          <el-input v-model="form.repassword" type="password" placeholder="请再次输入注册的密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" style="width: 100%;">注册</el-button>
          <el-link type="info" @click="$router.push('/login')">去登录</el-link>
        </el-form-item>
      </el-form>

    </div>
  </div>
  <!-- 表单区域 -->
</template>

<script>
import { registerAPI } from '@/api/index.js'
export default {
  name: 'my-Register',
  data() {
    const samePwd = (rule, value, callback) => {
      if (value !== this.form.password) {
        // 如果验证失败，则调用 回调函数时，指定一个 Error 对象。
        callback(new Error('两次输入的密码不一致!'))
      } else {
        // 如果验证成功，则直接调用 callback 回调函数即可。
        callback()
      }
    }
    return {
      form: {
        username: '',
        password: '',
        repassword: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,10}$/,
            message: '用户名必须是1-10的大小写字母数字',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '密码必须是6-15的非空字符',
            trigger: 'blur'
          }
        ],
        repassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须是6-15的非空字符', trigger: 'blur' },
          { validator: samePwd, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    onSubmit() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          // 通过
          // 1调用接口 判断
          const { data: res } = await registerAPI(this.form)
          // console.log(res)
          // 判断code是否为0
          if (res.code === 1) return this.$message.error(res.message)
          this.$message.success(res.message)
          this.$router.push('/login')
        } else {
          // 不通过
          return false
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
// 整体的盒子
.login-container {
  position: relative;
  height: 100%;
  background: url('../../assets/images/login_bg.jpg') center;
  background-size: cover;

  // 注册的盒子
  .login-box {
    position: absolute;
    width: 40rem;
    height: 36rem;
    top: calc(50% - 18rem);
    left: calc(50% - 20rem);
    background-color: #fff;
    border-radius: 2rem;
    box-sizing: border-box;
    padding: 0 3rem;
    z-index: 2;

    .title-box {
      margin-top: 1rem;
      height: 6rem;
      background: url("../../assets/images/login_title.png") center no-repeat;
    }
  }
}
</style>
